using UnityEditor;
using UnityEngine;
using System.IO;
using System.Collections.Generic;

namespace FbxToPrefab
{
    public static class Menu
    {
        [MenuItem("Tools/fbx处理/生成选中物体的prefab")]
        private static void CreatePrefabsFromFBX()
        {
            // 获取所有选中的对象
            Object[] selectedAssets = Selection.GetFiltered<Object>(SelectionMode.Assets);
            List<Object> processedAssets = new List<Object>();

            foreach (Object asset in selectedAssets)
            {
                string assetPath = AssetDatabase.GetAssetPath(asset);
                // 检查是否为FBX文件
                if (Path.GetExtension(assetPath).ToLower() == ".fbx")
                {
                    GameObject fbxObj = asset as GameObject;
                    if (fbxObj == null) continue;
                    var suc = Handle.TryMakePrefabByFbx(fbxObj, assetPath, out var prefab);
                    if (suc)
                    {
                        processedAssets.Add(prefab);
                    }
                }
            }

            // 刷新资源数据库并高亮显示新创建的Prefab
            AssetDatabase.Refresh();
            if (processedAssets.Count > 0)
            {
                Selection.objects = processedAssets.ToArray();
                EditorGUIUtility.PingObject(Selection.activeObject);
            }
        }
    }
}